package com.vgemv.jsutilitysdk.Drawable;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.Size;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class JsDrawableManager {

    private static JsDrawableManager _sharedInstance = new JsDrawableManager();

    JsDrawableManager() {

    }


    public static JsDrawableManager sharedInstance() {
        return _sharedInstance;
    }

    /*
    1 圆角矩形Drawbl
    e*/
    public static Drawable getRoundRectDrawableFill(final Size size,final Integer cornerRadius, final Integer fillColor) {
        return getRoundRectDrawable(size,null,cornerRadius,null,fillColor);
    }
//    public static Drawable getRoundRectDrawableStroke(final Size size, final Integer borderColor, final Integer borderWidth, final Integer cornerRadius, final Integer strokeColor, final Integer fillColor) {
//
//    }
    public static Drawable getRoundRectDrawable(final Size size, final Integer borderWidth, final Integer cornerRadius, final Integer strokeColor, final Integer fillColor) {
        return new Drawable() {
            @Override
            public void draw(@NonNull Canvas canvas) {
                Paint paint = new Paint();


                int width = size.getWidth();
                int height = size.getHeight();

                float padding = 0;
                if(borderWidth != null) {
                    padding = borderWidth / 2.0f;
                }
                RectF rec = new RectF(padding, padding, width-padding, height-padding);
                float _cornerRadius = cornerRadius!=null?cornerRadius:0;

                //绘制背景色
                if(fillColor != null) {
                    paint.setColor(fillColor);
                    paint.setStyle(Paint.Style.FILL);
                    canvas.drawRoundRect(rec, _cornerRadius, _cornerRadius, paint);

                }

                //绘制边框
                if(borderWidth != null && borderWidth > 0 && strokeColor != null) {
                    paint.setColor(strokeColor);
                    paint.setStyle(Paint.Style.STROKE);
                    paint.setStrokeWidth(borderWidth);
                    canvas.drawRoundRect(rec, _cornerRadius, _cornerRadius, paint);
                }
            }

            @Override
            public void setAlpha(int i) {

            }

            @Override
            public void setColorFilter(@Nullable ColorFilter colorFilter) {

            }

            @Override
            public int getOpacity() {
                return 0;
            }
        };
    }


    //圆形纯色图片
    //Color.parseColor,
    public Drawable circleDrawable(final int color, final int radius) {
        return new Drawable() {
            @Override
            public void draw(@NonNull Canvas canvas) {
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setColor(color);
                paint.setStyle(Paint.Style.FILL);
                RectF rec = new RectF(0, 0, radius, radius);
                canvas.drawRoundRect(rec, radius, radius, paint);
            }

            @Override
            public void setAlpha(int alpha) {

            }

            @Override
            public void setColorFilter(@Nullable ColorFilter colorFilter) {

            }

            @Override
            public int getOpacity() {
                return PixelFormat.UNKNOWN;
            }
        };
    }

}
